<template>
  <div class="comment-post">
    <van-field
      class="post-field"
      v-model.trim="message"
      rows="2"
      autosize
      type="textarea"
      maxlength="50"
      placeholder="请输入留言"
      show-word-limit
    />
    <van-button class="post-btn" :disabled="message.length===0" @click="onPost">发布</van-button>
  </div>
</template>

<script>
// import { emit } from '@/utils/eventBus'
import { addComment } from '@/api/comment'
export default {
  name: 'CommentPost',
  components: {},
  props: {
    target: {
      type: [Number, String, Object],
      required: true
    }
  },
  inject: {
    articleId: {
      type: [Number, String, Object]
    }
  },
  data () {
    return {
      message: ''
    }
  },
  computed: {},
  watch: {},
  created () {},
  mounted () {},
  methods: {
    async onPost () {
      const { data: { data: { new_obj: comment } } } = await addComment({
        target: this.target,
        content: this.message,
        art_id: this.articleId
      })
      this.$toast.success('发布成功')
      //  清空输入框内容
      this.message = ''
      //   方法二
      //  关闭弹层 ----数据在父组件,isPostShow=false
      this.$emit('onpost-success', comment) // 通过父子通信
      // 添加数据到评论列表 ---数据在兄弟组件(comment-list),李斯特.unshift()
      // 方式一 通过eventBus事件总线
      //   emit('onpost-success', comment)
    }
  }
}
</script>

<style scoped lang="less">
.comment-post {
  display: flex;
  align-items: center;
  padding: 32px 0 32px 32px;
  .post-field {
    background-color: #f5f7f9;
  }
  .post-btn {
    width: 150px;
    border: none;
    padding: 0;
    color: #6ba3d8;
    &::before {
      display: none;
    }
  }
}
</style>
